package com.kgc.server.service.Impl;

import com.alibaba.fastjson.JSON;
import com.kgc.server.dao.UserDao;
import com.kgc.server.pojo.Address;
import com.kgc.server.pojo.Page;
import com.kgc.server.pojo.User;
import com.kgc.server.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service("userService")
public class UserServiceImpl implements UserService {
    Logger logger = Logger.getLogger(getClass());
    @Autowired
    private UserDao userDao;

    @Autowired
    RedisTemplate<Object, Object> redisTemplate;

    public User getRedisUserInfoList(User user){
        logger.info("getRedisUserInfoList user:"+user);
        int abc = 0;
        //数据库找值
        List new_list = getUserList();
        System.out.println(new_list);
        for(int i=0;i<new_list.size();i++){
            User user2 = (User)new_list.get(i);
            if( user2.getUserName().equals(user.getUserName()) ){
                if( user2.getPassword().equals(user.getPassword()) ){
                    //存入redis
                    redisTemplate.opsForValue().set(user2.getUserName(), JSON.toJSONString(user2),30, TimeUnit.MINUTES);
                    logger.info("getRedisUserInfoList user:"+user+",user2:"+user2);
                    return user2;
                }
            }else{
                abc++;
            }
        }
        if( abc == new_list.size() ){
            return null;
        }
        return null;
    }


    @Override
    public List getRegular() {
        logger.info("getRegular start...");
        List list = userDao.getRegular();
        return list;
    }

    @Override
    public List getUserList() {
        logger.info("getUserList start...");
        return userDao.getUserList();
    }

    @Override
    public int setUserList(User user) {
        logger.info("setUserList user:"+user);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        String time = simpleDateFormat.format(date);
        logger.info("setUserList user:"+user+",time:"+time);
        user.setRegistrationTime(time);
        int num = userDao.setUserList(user);
        logger.info("setUserList user:"+user+",time:"+time+",num:"+num);
        return num;
    }


    @Override
    public List getUserListFy(Page page) {
        logger.info("getUserListFy page:"+page);
        //设置总数
        int num = userDao.getCount();
        page.setCount(num);
        logger.info("getUserListFy page:"+page+",num:"+num);
        if( page.getCount() % page.getPageSize() == 0 ){
            page.setPageCount(page.getCount() / page.getPageSize());
        }else{
            page.setPageCount(page.getCount() / page.getPageSize() + 1);
        }
        page.setNum((page.getPageNo()-1)*page.getPageSize());
        List list = userDao.getUserListFy(page);
        logger.info("getUserListFy page:"+page+",num:"+num+",list:"+list);
        return list;
    }

    @Override
    public int delUserListById(int id) {
        logger.info("delUserListById id:"+id);
        int num = userDao.delUserListById(id);
        logger.info("delUserListById id:"+id+",num:"+num);
        return num;
    }

    @Override
    public List<User> getUserListByID(int id) {
        logger.info("getUserListByID id:"+id);
        List<User> list = userDao.getUserListByID(id);
        logger.info("getUserListByID id:"+id+",list:"+list);
        return list;
    }

    @Override
    public int updateUserListByID(User user) {
        logger.info("updateUserListByID user:"+user);
        int num = userDao.updateUserListByID(user);
        logger.info("updateUserListByID user:"+user+",num:"+num);
        return num;
    }

    public int setAddressById(Address address){
        logger.info("setAddressById address:"+address);
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh-mm-ss");
        String time = simpleDateFormat.format(date);
        address.setCreateTime(time);
        logger.info("setAddressById address:"+address+",time:"+time);
        int num = userDao.setAddressById(address);
        logger.info("setAddressById address:"+address+",time:"+time+",num:"+num);
        return num;
    }

    @Override
    public List getAddress(int userId) {
        logger.info("getAddress userId:"+userId);
        return userDao.getAddress(userId);
    }

    @Override
    public List getAddressById(int id) {
        logger.info("getAddressById id:"+id);
        return userDao.getAddressById(id);
    }

    @Override
    public int updataAddressById(Address address) {
        logger.info("updataAddressById address:"+address);
        int num = userDao.updataAddressById(address);
        logger.info("updataAddressById address:"+address+",num:"+num);
        return num;
    }

    @Override
    public int updataMrAddress(int id) {
        logger.info("updataMrAddress id:"+id);
        int num = userDao.updataMrAddress(id);
        logger.info("updataMrAddress id:"+id+",num:"+num);
        return num;
    }

    @Override
    public int delAddressById(int id) {
        logger.info("delAddressById id:"+id);
        int num = userDao.delAddressById(id);
        logger.info("delAddressById id:"+id+",num:"+num);
        return num;
    }

    @Override
    public int updataAll(int userId) {
        logger.info("updataAll userId:"+userId);
        int num = userDao.updataAll(userId);
        logger.info("updataAll userId:"+userId+",num:"+num);
        return num;
    }

    @Override
    public int setUserPassWordById(String email, String password,String userName) {
        logger.info("setUserPassWordById id:"+email+",password:"+password);
        int num = userDao.setUserPassWordById(email,password,userName);
        logger.info("setUserPassWordById id:"+email+",password:"+password+",num:"+num);
        return num;
    }

}
